package net.yambal.android.util;

import java.util.ArrayList;
import java.util.List;

public class ArrayUtil {
    public static String join(String[] arry, String with) {
        if(arry != null){
        	StringBuffer buf = new StringBuffer();
	        for (String s: arry) {
	            if (buf.length()>0) {
	                buf.append(with);
	            }
	            buf.append(s);
	         }
	         return buf.toString();
        }else{
        	return "";
        }
    }
    
    public static List<String> CSVToList(String str){
    	String[] arr = CSVToArray(str);
    	if(arr != null){
	    	List<String> ret = new ArrayList<String>();
	    	for(int i = 0; i < arr.length; i++){
	    		ret.add(arr[i]);
	    	}
	    	return ret;
    	}
    	return null;
    }

    public static String ListToCSV(List<String> list){
    	return ListJoin(list, ",");
    }
    public static String ListJoin(List<String> list, String grue){
    	int i = 0;
    	String ret = "";
    	while(list.size() != 0){
    		if(ret.length() != 0){
    			ret += grue;
    		}
    		ret += list.remove(i);
    	}
    	return ret;
    }
    
    public static String[] CSVToArray(String csv){
    	if(csv != null){
    		String[] arr = csv.split(",");
    		for(int i = 0; i < arr.length; i ++){
    			arr[i] = arr[i].trim();
    		}
    		return arr;
    	}
    	return null;
    }
}
